﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// Program.cs:
//
// Aplicativo de debug e restauração e acesso a funções wbfs via console
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DevConsole
{
    class Program
    {
        //-----------------
        delegate void CommandDelegate(String[] args);

        //----------------- Variáveis
        static Dictionary<String, CommandDelegate> Commands = new Dictionary<string, CommandDelegate>();
        static Boolean exit = false;

        //-----------------Rotinas

        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //

            Commands.Add("help", new CommandDelegate(ListCommands));
            Commands.Add("exit", new CommandDelegate(ExitCommand));

            if ((args.Length == 1)&&(args[0] == "-dev"))
            {
                args = new string[0];

                Commands.Add("verify", new CommandDelegate(DeviceVerify.VerifyCommand));
                Commands.Add("dump", new CommandDelegate(DeviceDump.DumpCommand));
                Commands.Add("makerelease", new CommandDelegate(UpdateCreator.UpdateCommand));
            }

            Commands.Add("listgames", new CommandDelegate(Operations.CommList));
            Commands.Add("add", new CommandDelegate(Operations.CommAdd));
            Commands.Add("extract", new CommandDelegate(Operations.CommExtract));
            Commands.Add("scrub", new CommandDelegate(Operations.CommScrub));
            Commands.Add("unscrub", new CommandDelegate(Operations.CommUnscrub));
            Commands.Add("unciso", new CommandDelegate(Operations.CommUnCIso));
            Commands.Add("updatescrub", new CommandDelegate(Operations.CommUpdateScrub));
            Commands.Add("delete", new CommandDelegate(Operations.CommDelete));
            Commands.Add("rename", new CommandDelegate(Operations.CommRename));
            Commands.Add("format", new CommandDelegate(Operations.CommFormat));
            
            //

            LoadMainScreen();

            //Executa qualquer comando que tenha sido passado através de 'args'
            if (args.Length > 0)
            {
                Console.Write("\n>");
                for (int i = 0; i < args.Length; i++) Console.Write(args[i]);
                Console.WriteLine();

                if (Commands.ContainsKey(args[0])) Commands[args[0]](args);
                else Console.WriteLine("Unknown command");
            }
            //

            while (!exit)
            {
                Console.Write("\n>");

                string command = Console.ReadLine();
                List<String> arguments = 
                    new List<string>(command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                if (arguments.Count == 0) continue;

                //Une argumentos dentro de aspas
                if (command.Contains('"'))
                {
                    for (int i = 0; i < arguments.Count; i++)
                    {
                        if (arguments[i][0] != '"') continue;
                        arguments[i] = arguments[i].Remove(0, 1);

                        //O final das aspas está no mesmo argumento?
                        int l = arguments[i].Length;
                        if (arguments[i][l - 1] == '"')
                        {
                            arguments[i] = arguments[i].Remove(l - 1);
                            break;
                        }

                        //
                        while (i < arguments.Count - 1)
                        {
                            l = arguments[i + 1].Length;
                            if (arguments[i + 1][l - 1] == '"')
                            {
                                arguments[i] += ' ' + arguments[i + 1].Remove(l - 1);
                                arguments.RemoveAt(i + 1);
                                break;
                            }
                            else
                            {
                                arguments[i] += ' ' + arguments[i + 1];
                                arguments.RemoveAt(i + 1);
                            }
                        }
                    }
                }

                if (Commands.ContainsKey(arguments[0])) Commands[arguments[0]](arguments.ToArray());
                else Console.WriteLine("Unknown command");
            }
        }

        //-------------------

        static void ListCommands(String[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Command list:");
            for (int i = 0; i < Commands.Count; i++)
            {
                Console.WriteLine(Commands.ElementAt(i).Key);
            }

            Console.WriteLine();
            Console.WriteLine("Note: Composite names(space separated) must be grouped with \"\"");
        }

        //-------------------

        static void ExitCommand(String[] args)
        {
            exit = true;
        }

        //-------------------

        internal static void LoadMainScreen()
        {
            Console.Clear();
            Console.ResetColor();

            Console.WriteLine("╔═══════════════════════════════════════════════════════╗");
            Console.WriteLine("║ WBFSSync 2 - Development Console                      ║");
            Console.WriteLine("║                                                       ║");
            Console.WriteLine("║ type 'help' to show available command list            ║");
            Console.WriteLine("╚═══════════════════════════════════════════════════════╝");
        }

        //-------------------

        internal static void DrawLine(int width)
        {
            for (int i = 0; i < width; i++) Console.Write('═');
        }
    }
}
